package cn.kgc.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cn.kgc.util.ConfigManager;

//数据库操作的基类
public class BaseDao {
	Connection connection = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;
	
	//获取数据库连接
	public boolean getConnection(){
		//加载不同数据库厂商提供的驱动
		try {
//			Class.forName("com.mysql.jdbc.Driver");
			Class.forName(ConfigManager.getInstance().getString("jdbc.driver"));//使用单例模式，与上句作用相同
			//查询来说
			//(1)铺路（获取连接connection）
//			String url = "jdbc:mysql://localhost:3306/kgcnews";
			String url = ConfigManager.getInstance().getString("jdbc.connection.url");
//			connection = DriverManager.getConnection(url, "root", "1234");
			String username = ConfigManager.getInstance().getString("jdbc.connection.username");
			String password = ConfigManager.getInstance().getString("jdbc.connection.password");
			connection = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	//通过数据源获取数据库连接
	public boolean getConnection2(){
		try {
			//初始化上下文
			Context cxt = new InitialContext();
			//获取与逻辑名称相关联的数据源对象
			DataSource ds = (DataSource)cxt.lookup("java:comp/env/jdbc/news");
			//通过数据源获取数据库连接
			connection = ds.getConnection();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}
	
	//增删改
	//String sql = "update news_detail set name=? where id=?";
	//params = {"张三",3};
	//pstmt.setString(1,params[0]);
	//pstmt.setInt(2,params[1]);
	public int executeUpdate(String sql,Object[] params){
		int updateRows = 0;
		if(this.getConnection()){
			try {
				pstmt = connection.prepareStatement(sql);
				//填充占位符
				for(int i=0;i<params.length;i++){
					pstmt.setObject(i+1, params[i]);
				}
				updateRows = pstmt.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return updateRows;
	}
	
	//查
	public ResultSet executeSQL(String sql,Object[] params){
		if(this.getConnection()){
			try {
				pstmt = connection.prepareStatement(sql);
				//填充占位符
				for(int i=0;i<params.length;i++){
					pstmt.setObject(i+1, params[i]);
				}
				rs = pstmt.executeQuery();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}
	
	//查:通过数据源获取连接
		public ResultSet executeSQL2(String sql,Object[] params){
			if(this.getConnection2()){
				try {
					pstmt = connection.prepareStatement(sql);
					//填充占位符
					for(int i=0;i<params.length;i++){
						pstmt.setObject(i+1, params[i]);
					}
					rs = pstmt.executeQuery();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			return rs;
		}
	
	//释放资源(注意释放资源的顺序)
	public boolean closeResource(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}
		}
		if(pstmt!=null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}
		}
		if(connection!=null){
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
}
